package com.fesine.blog.controller;

import com.fesine.blog.domain.EsBlog;
import com.fesine.blog.repository.EsBlogRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * @description: 博客控制器
 * @author: Fesine
 * @createTime:2017/12/17
 * @update:修改内容
 * @author: Fesine
 * @updateTime:2017/12/17
 */
@Controller
@RequestMapping("/blogs")
public class BlogController {

    @Autowired
    private EsBlogRepository esBlogRepository;

    private static final Logger logger = LoggerFactory.getLogger(BlogController.class);

    @GetMapping
    public String listBlogs(@RequestParam(value = "order",required = false,defaultValue = "new")String order,
                            @RequestParam(value = "tag",required = false)String tag){
        logger.info("order = {}; tag = {}",order, tag);
        return "redirect:/index?order="+order+"&tag="+ tag;
    }

    @GetMapping("/list")
    public List<EsBlog> list(
            @RequestParam(value = "title")String title,
            @RequestParam(value = "summary")String summary,
            @RequestParam(value = "content")String content,
            @RequestParam(value = "pageIndex",defaultValue = "0")int pageIndex,
            @RequestParam(value = "pageSize",defaultValue = "10")int pageSize
    ){
        Pageable page = new PageRequest(pageIndex,pageSize);
        Page<EsBlog> pages =
                esBlogRepository
                        .findDistinctEsBlogByTitleContainingOrSummaryContainingOrContentContaining(
                title, summary, content, page
        );
        return pages.getContent();
    }
}
